using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Utilities;
using System.Management;
using System.Diagnostics;
using Microsoft.Build.Framework;

namespace BizTalk.BuildGenerator.Tasks
{
    /// <summary>
    /// Task to stop hosts
    /// </summary>
    public class StopHost : Task
    {
        string _HostName;

        #region Properties
        /// <summary>
        /// HostName
        /// </summary>
        [Required]
        public string HostName
        {
            get { return _HostName; }
            set { _HostName = value; }
        }
        #endregion

        /// <summary>
        /// Execute method
        /// </summary>
        /// <returns></returns>
        public override bool Execute()
        {
            try
            {
                ManagementObject hostObject = HostsHelper.GetHostObject(this._HostName);
                hostObject.InvokeMethod("Stop", null, null);

                return true;
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Stop host failed: " + ex.ToString());
                throw;
            }
        }
    }
}
